import 'package:flutter/material.dart';

import './page2.dart';

class Home extends StatelessWidget {
  const Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
        title: Text("首页"),
      ) ,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("跳转按钮"),
            RaisedButton(
              child: Text("跳转到Page1"),
              padding: EdgeInsets.all(10),
              color: Theme.of(context).accentColor,
              textTheme: ButtonTextTheme.primary,
              onPressed: (){
                Navigator.of(context).pushNamed('/page1');
              },
            ),
            RaisedButton(
              child: Text("未定义页面"),
              padding: EdgeInsets.all(10),
              color: Theme.of(context).accentColor,
              textTheme: ButtonTextTheme.primary,
              onPressed: (){
                Navigator.of(context).pushNamed('/not define');
              },
            ),
            RaisedButton(
              child: Text("从Page2获取数据"),
              padding: EdgeInsets.all(10),
              color: Theme.of(context).accentColor,
              textTheme: ButtonTextTheme.primary,
              onPressed: () async {
                // 回传值的类型不要限定；这里如果限定返回值是String，会抛异常
                // String value = await Navigator.of(context).pushNamed('/page2');
                // type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String>'
                try {
                  final value = await Navigator.of(context).pushNamed('/page2');
                  print("收到回传的数据：$value");
                } catch (e){
                  print("异常：$e");
                }
              },
            ),
            RaisedButton(
              child: Text("无参数跳转到Page3"),
              padding: EdgeInsets.all(10),
              color: Theme.of(context).accentColor,
              textTheme: ButtonTextTheme.primary,
              onPressed: (){
                Navigator.of(context).pushNamed('/page3');
              },
            ),
            RaisedButton(
              child: Text("带参数跳转到Page3"),
              padding: EdgeInsets.all(10),
              color: Theme.of(context).accentColor,
              textTheme: ButtonTextTheme.primary,
              onPressed: (){
                Navigator.of(context).pushNamed('/page3', arguments: {
                  "title": "透传title",
                  "name": 'postbird',
                  'password': '123456'
                });
              },
            ),
          ],
        ),
      ),

    );
  }
}